import { ProTable, ActionType } from '@ant-design/pro-components';
import React, { useRef } from 'react';
import { message, Space, Popconfirm, Button, Select, Modal } from 'antd';
import { useRequest } from 'ahooks';
import { adminV1AdminControllerUpdate, adminV1AdminControllerIndex } from '@/services/auto/adminAdmin';
import Add from './component/Add';
import { getEnumWithSearch } from "@/utils/protable-column";
import { BooleanNumber } from "common/utils/data";

export default () => {
  const actionRef = useRef<ActionType>();

  const { runAsync: updateRun } = useRequest(adminV1AdminControllerUpdate, {
    manual: true,
    onSuccess: () => {
      message.success('操作成功');
      actionRef.current?.reload();
    },
  });

  return (
    <>
      <ProTable<API.AdminIndexResult, API.AdminV1AdminControllerIndexParams>
        actionRef={actionRef}
        rowKey="id"
        toolBarRender={() => [<Add key="add" reload={()=>{
          actionRef.current?.reload();
        }}>
          <Button type="primary">
            添加管理员
          </Button>
        </Add>]}
        request={(params) => {
          const {pageSize, current, status} = params;
          return adminV1AdminControllerIndex({
            pageSize,
            pageIndex: current,
            status,
          }).then((res) => {
            return {
              success: true,
              data: res.list,
              total: res.total,
            };
          });
        }}
        columns={[
          {
            title: '账号名',
            dataIndex: 'username',
            search: false,
          },
          {
            title: '姓名',
            dataIndex: 'name',
            search: false,
          },
          {
            title: '手机',
            dataIndex: 'mobile',
            search: false,
          },
          {
            title: '职位',
            dataIndex: 'job',
            search: false,
          },
          {
            title: '关联角色',
            dataIndex: 'roleNames',
            search: false,
            render: (val, record)=>{
              return record.roleNames.join('、')
            }
          },
          {
            title: '添加时间',
            valueType: 'dateTime',
            dataIndex: 'createTime',
            search: false,
          },
          ...getEnumWithSearch({
            title: '账号状态',
            dataIndex: 'status',
            options: [
              {
                label: '启用',
                value: BooleanNumber.true,
              },
              {
                label: '禁用',
                value: BooleanNumber.false,
              }
            ]
          }),
          {
            align: 'center',
            title: '操作',
            key: 'option',
            valueType: 'option',
            width: 120,
            render: (_, record) => {
              return (
                <Space direction="vertical" size={0}>
                  <Add
                    reload={actionRef.current?.reload}
                    data={{
                      ...record,
                    }}
                  >
                    <Button type="link">
                      编辑
                    </Button>
                  </Add>
                  {
                    record.id !== 1 && <>
                      {
                        record.status === BooleanNumber.true ? <Popconfirm
                          title="冻结后账号不可登录，确定冻结当前账号？"
                          okText="确认冻结"
                          onConfirm={() => {
                            return updateRun({uuid: record.uuid},{status: BooleanNumber.false});
                          }}
                        >
                          <Button type="link" danger>
                            冻结
                          </Button>
                        </Popconfirm> : <Popconfirm
                          title="确定解冻当前账号？"
                          onConfirm={() => {
                            return updateRun({uuid: record.uuid},{status: BooleanNumber.true});
                          }}
                        >
                          <Button type="link">
                            解冻
                          </Button>
                        </Popconfirm>
                      }
                    </>
                  }
                </Space>
              );
            },
          },
        ]}
      />
    </>
  );
};
